package cn.cpf.tool.doc.word;

import lombok.Cleanup;
import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <b>Description : </b>
 *
 * @author CPF
 * Date: 2020/4/17 15:53
 */
public class OfficeReader {

    public static void main(String[] args) {
//        String name = "D:\\Users\\CPF\\Desktop\\金融市场业务系统完善项目\\开发指引doc\\技术01.中国外汇交易中心新一代交易平台Maker会员接口IMIX协议开发指引_V0.2.docx";
        String name = "D:\\Users\\CPF\\Desktop\\金融市场业务系统完善项目\\开发指引doc\\中国外汇交易中心交易后处理接口IMIX协议开发指引V1.7.docx";
        String name2 = "D:\\Users\\CPF\\Desktop\\FMBS\\resources\\CFETS 开发指引\\接口开发指引-本币\\新平台本币交易接口\\第2卷-现券买卖市场V3.3.4.pdf";
        readWordContent(name);
//        readWord("cstp", name);
    }


    /**
     * word
     *
     * @param filePath 文件路径
     */
    public static void readWord(String tag, String filePath){
        try (FileInputStream in = new FileInputStream(filePath)){
            //载入文档 //如果是office2007  docx格式
            if(filePath.toLowerCase().endsWith("docx")){
                //word 2007 图片不会被读取， 表格中的数据会被放在字符串的最后
                //得到word文档的信息
                @Cleanup XWPFDocument xwpf = new XWPFDocument(in);

                Iterator<IBodyElement> bodyElementsIterator = xwpf.getBodyElementsIterator();
                IBodyElement last, element = null;
                String title = "null";
                int rowNumber = 0;
                Set<String> headFieldSet = new HashSet<>();
                while (bodyElementsIterator.hasNext()) {
                    last = element;
                    element = bodyElementsIterator.next();
                    if (element instanceof XWPFTable) {
                        assert (last instanceof XWPFParagraph);
                        String titleTmp = ((XWPFParagraph) last).getRuns().stream().map(item -> item.toString().trim()).collect(Collectors.joining("-"));
                        if (!titleTmp.trim().isEmpty()) {
                            System.out.println(" \n\n ");
                            title = titleTmp;
                            rowNumber = 0;
//                            System.out.println(title);
                        }
                        String finalTitle = title;
                        List<XWPFTableRow> rows = ((XWPFTable) element).getRows();
                        for (XWPFTableRow row : rows) {
                            String rowText = row.getTableCells().stream().map(item -> item.getText().trim().replaceAll("\\n", "\\n").replaceAll("\\t", "\\t")).collect(Collectors.joining("\t"));
                            if (rowNumber == 0) {
                                row.getTableCells().stream().map(item -> item.getText().trim()).forEach(headFieldSet::add);
                            }
                            System.out.println(tag + "\t" + finalTitle + "\t" + rowNumber ++ + "\t" + rowText);
                        }
                    }
                }
                System.out.println(headFieldSet);
            } else if(filePath.toLowerCase().endsWith("doc")){

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * word
     *
     * @param filePath 文件路径
     */
    public static void readWordContent(String filePath){
        try (FileInputStream in = new FileInputStream(filePath)){
            //载入文档 //如果是office2007  docx格式
            if(filePath.toLowerCase().endsWith("docx")){
                //word 2007 图片不会被读取， 表格中的数据会被放在字符串的最后
                //得到word文档的信息
                @Cleanup XWPFDocument xwpf = new XWPFDocument(in);

                Iterator<IBodyElement> bodyElementsIterator = xwpf.getBodyElementsIterator();
                IBodyElement last, element = null;
                String title = "null";
                int rowNumber = 0;
                Set<String> headFieldSet = new HashSet<>();
                while (bodyElementsIterator.hasNext()) {
                    last = element;
                    element = bodyElementsIterator.next();
                    if (element instanceof XWPFTable) {
//                        assert (last instanceof XWPFParagraph);
//                        String titleTmp = ((XWPFParagraph) last).getRuns().stream().map(item -> item.toString().trim()).collect(Collectors.joining("-"));
//                        if (!titleTmp.trim().isEmpty()) {
//                            System.out.println(" \n\n ");
//                            title = titleTmp;
//                            rowNumber = 0;
////                            System.out.println(title);
//                        }
//                        String finalTitle = title;
//                        List<XWPFTableRow> rows = ((XWPFTable) element).getRows();
//                        for (XWPFTableRow row : rows) {
//                            String rowText = row.getTableCells().stream().map(item -> item.getText().trim().replaceAll("\\n", "\\n").replaceAll("\\t", "\\t")).collect(Collectors.joining("\t"));
//                            if (rowNumber == 0) {
//                                row.getTableCells().stream().map(item -> item.getText().trim()).forEach(headFieldSet::add);
//                            }
//                            System.out.println(finalTitle + "\t" + rowNumber ++ + "\t" + rowText);
//                        }
                    } else if (element instanceof  XWPFParagraph){
                        String titleTmp = ((XWPFParagraph) element).getRuns().stream().map(item -> item.toString().trim()).collect(Collectors.joining("#@@#"));
                        System.out.println(titleTmp);
                    }
                }
                System.out.println(headFieldSet);
            } else if(filePath.toLowerCase().endsWith("doc")){

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
